package Options::UserSync::API::UserRegistration;

use strict;
use DE_EPAGES::Core::API::Error qw (Error );
use DE_EPAGES::Trigger::API::Trigger qw ( TriggerHook );
use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);
use DE_EPAGES::Object::API::Factory qw( LoadObject ExistsObjectByPath LoadObjectByPath Transaction DeleteObject );
use DE_EPAGES::Order::API::LineItemContainer qw(SaveShipping);
use DE_EPAGES::Object::API::Object::Object qw(clone insertChild);
use Data::Dumper;

use Options::Attributes::API::InterfaceCustomerUpdate;
use Options::UserSync::API::AddressFieldsConverter;
use Options::UserSync::API::ShopperID;

sub APIInsertCustomerUser {
                my ($aParams) = @_;
                my $CustomerID = $aParams->{CustomerID};
                my $UserID = $aParams->{UserID};
                GetLog->debug("APIInsertCustomerUser - INVOKED");
                my $CustomerOBJ = LoadObject($CustomerID);
                my $User = LoadObject($UserID);       
                my $OptionsCustId = $CustomerOBJ->get('OptionsCustId');
                my $shop = $CustomerOBJ->get('Shop');
                my $OptionsShopperID = &Options::UserSync::API::ShopperID::getNewShopperID($shop);
                $CustomerOBJ->set({ 'OptionsShopperID' => $OptionsShopperID }); 
                my $CustomerShopperID = $CustomerOBJ->get('OptionsShopperID'); 
                
                if (!defined($OptionsCustId) || $OptionsCustId == 0) {
                GetLog->debug("APIInsertCustomerUser - INVOKED - and proceeding.....");
                
                
                my $BillingAddress = $User->get('BillingAddress');
                GetLog->debug("APIInsertCustomerUser - TRIGERED: $UserID ");
                my %Customer; # will use autovivification feature to create nice customer record for Options - thank you PERL.
                my $FieldsConverter = new Options::UserSync::API::AddressFieldsConverter();
                $FieldsConverter->convertToOptions($BillingAddress, \%Customer, "billing");
                GetLog->debug("API_InsertCustomerUser - 1 - Customer: " . Dumper(\%Customer));
                #In terms of shipping address we are taking advantage of the fact that new registered user can have only one shipping
                #address and if defined it is always assigned as selected shipping address.
                my $ShippingAddress = $User->get('ShippingAddress');
                if (defined($ShippingAddress)) {
                                $FieldsConverter->convertToOptions($ShippingAddress, \%Customer, "shipping");
                                GetLog->debug("API_InsertCustomerUser - 2  - Customer: " . Dumper(\%Customer));
                }
               
                %Customer->{ShopperID} = $CustomerShopperID;
                %Customer->{Modified} = 'true';
                %Customer->{Type} = 7;
                
                #And here we go:
                GetLog->debug("API_InsertCustomerUser - 3  - Customer: " . Dumper(\%Customer));
         
                my $WSLinkUpdate = Options::Attributes::API::InterfaceCustomerUpdate::new("Options::Attributes::API::InterfaceCustomerUpdate",
                                                                                            "CustomerUpdate",
                                                                                            "CustomerUpdateRequest",
                                                                                             \%Customer,
                                                                                            "");
                my $Result = $WSLinkUpdate->hitMe();
                #Normally $Result is null when operation is successful 
                GetLog->debug("API_InsertCustomerUser - TRIGERED Customer: " . $CustomerID . " User: " . $UserID . " RESULT: " . Dumper($Result));

#THIS IS THE PART OF IMMEDIATE SYNC BACK OF THE NEW USER DATA FROM OPTIONS WITH CUSTOMERID
#BUT IT IS NOT SUPPORTED BY OPTIONS - EVEN IF IT DOESN'T WORK INSTANTLY - NO POINT MAKING IT HERE
# THERE IS ONE MORE DOUBT THAT THIS HOOK IS NOT GOOD FOR UPDATE CUSTOMER DATA ANYWAY.        

                my %UserSearchRequest = (
                                       # 'Surname'               => %Customer->{'Surname'},
                                       # 'Postcode'              => %Customer->{'Postcode'},
                                        'ShopperID'             => %Customer->{'ShopperID'},
                                        'GetAllCustomers'       => 'false',
                );
                
                GetLog->debug("API_InsertCustomerUser - Customer request: " . Dumper(\%UserSearchRequest));
                
                
                my $WSLinkGet = Options::Attributes::API::InterfaceCustomerGet::new("Options::Attributes::API::InterfaceCustomerGet",
                                                                                            "CustomerSearch",
                                                                                            "CustomerSearchRequest",
                                                                                             \%UserSearchRequest,
                                                                                            "Customer");
                my $Customer = $WSLinkGet->hitMe();
                #Here we are trying to pick up all new details regarding customer
                #Unfortunately Options doesn't return anything in here.
                #Anyway this is the place where we should update customer ID from options.
                GetLog->debug("API_InsertCustomerUser - Customer respond: " . Dumper($Customer));
    
                } else {
                GetLog->debug("APIInsertCustomerUser - INVOKED - but did not do anything.");
                }
                
                 
}

1;